/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package quests.Q025_HidingBehindTheTruth;

import net.sf.l2j.gameserver.ai.CtrlIntention;
import net.sf.l2j.gameserver.model.actor.L2Npc;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.quest.Quest;
import net.sf.l2j.gameserver.model.quest.QuestState;
import net.sf.l2j.gameserver.model.quest.State;
import net.sf.l2j.gameserver.network.serverpackets.NpcSay;

public class Q025_HidingBehindTheTruth extends Quest
{
	private final static String qn = "Q025_HidingBehindTheTruth";
	
	//NPCs
	private static final int Agripel = 31348;
	private static final int Benedict = 31349;
	private static final int Wizard = 31522;
	private static final int Tombstone = 31531;
	private static final int Lidia = 31532;
	private static final int Bookshelf = 31533;
	private static final int Bookshelf2 = 31534;
	private static final int Bookshelf3 = 31535;
	private static final int Coffin = 31536;
	private static final int Triol = 27218;
	
	//Items
	private static final int Contract = 7066;
	private static final int Dress = 7155;
	private static final int SuspiciousTotem = 7156;
	private static final int GemstoneKey = 7157;
	private static final int TotemDoll = 7158;
	
	public Q025_HidingBehindTheTruth(int questId, String name, String descr)
	{
		super(questId,name,descr);
		
		questItemIds = new int[] { SuspiciousTotem, GemstoneKey, TotemDoll, Dress };
		
		addStartNpc(Benedict);
		addTalkId(Benedict, Agripel, Wizard, Tombstone, Lidia, Bookshelf, Bookshelf2, Bookshelf3, Coffin);
		
		addKillId(Triol);
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		if (event.equalsIgnoreCase("31349-02.htm"))
		{
			st.playSound("ItemSound.quest_accept");
			st.set("cond", "1");
			st.setState(State.STARTED);
		}
		else if (event.equalsIgnoreCase("31349-03.htm"))
		{
			if (st.getQuestItemsCount(SuspiciousTotem) == 1)
				htmltext = "31349-05.htm";
			else
			{
				st.playSound("ItemSound.quest_middle");
				st.set("cond", "2");
			}
		}
		else if (event.equalsIgnoreCase("31349-10.htm"))
		{
			st.playSound("ItemSound.quest_middle");
			st.set("cond", "4");
		}
		else if (event.equalsIgnoreCase("31348-02.htm"))
			st.takeItems(SuspiciousTotem, -1);
		else if (event.equalsIgnoreCase("31348-07.htm"))
		{
			st.playSound("ItemSound.quest_middle");
            st.set("cond", "5");
            st.giveItems(GemstoneKey, 1);
		}
		else if (event.equalsIgnoreCase("31522-04.htm"))
		{
			st.playSound("ItemSound.quest_middle");
            st.set("cond", "6");
		}
		else if (event.equalsIgnoreCase("31535-03.htm"))
		{
			L2Npc triol = st.addSpawn(Triol, 59712, -47568, -2712, 0);
			triol.broadcastPacket(new NpcSay(triol.getObjectId(), 0, triol.getNpcId(), "That box was sealed by my master. Don't touch it!"));
			triol.setRunning();
			//triol.addDamageHate(player, 0, 999);
			triol.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, player);
			st.playSound("ItemSound.quest_middle");
			st.set("cond", "7");
		}
		else if (event.equalsIgnoreCase("31535-05.htm"))
		{
			st.giveItems(Contract, 1);
            st.takeItems(GemstoneKey, -1);
            st.playSound("ItemSound.quest_middle");
            st.set("cond", "9");
		}
		else if (event.equalsIgnoreCase("31532-02.htm"))
			st.takeItems(Contract, -1);
		else if (event.equalsIgnoreCase("31532-06.htm"))
		{
			st.playSound("ItemSound.quest_middle");
            st.set("cond", "11");
		}
		else if (event.equalsIgnoreCase("31531-02.htm"))
		{
			st.playSound("ItemSound.quest_middle");
            st.set("cond", "12");
            st.addSpawn(Coffin, 60104, -35820, -664, 0);
		}
		else if (event.equalsIgnoreCase("31532-18.htm"))
		{
			st.playSound("ItemSound.quest_middle");
            st.set("cond", "15");
		}
		else if (event.equalsIgnoreCase("31522-12.htm"))
		{
			st.playSound("ItemSound.quest_middle");
            st.set("cond", "16");
		}
		else if (event.equalsIgnoreCase("31348-10.htm"))
			st.takeItems(TotemDoll, -1);
		else if (event.equalsIgnoreCase("31348-15.htm"))
		{
			st.playSound("ItemSound.quest_middle");
            st.set("cond", "17");
		}
		else if (event.equalsIgnoreCase("31348-16.htm"))
		{
			st.playSound("ItemSound.quest_middle");
            st.set("cond", "18");
		}
		else if (event.equalsIgnoreCase("31532-20.htm"))
		{
			st.giveItems(905, 2);
            st.giveItems(874, 1);
            st.takeItems(7063, -1);
            st.addExpAndSp(572277, 53750);
            st.exitQuest(false);
            st.playSound("ItemSound.quest_finish");
		}
		else if (event.equalsIgnoreCase("31522-15.htm"))
		{
			st.giveItems(936, 1);
            st.giveItems(874, 1);
            st.takeItems(7063, -1);
            st.addExpAndSp(572277, 53750);
            st.exitQuest(false);
            st.playSound("ItemSound.quest_finish");
		}
		
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		String htmltext = Quest.getNoQuestMsg(player);
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		if (st.isCompleted())
		{
			htmltext = Quest.getAlreadyCompletedMsg(player);
			return htmltext;
		}
		
		int cond = st.getInt("cond");
		
		switch (st.getState())
		{
		case State.CREATED:
			QuestState st2 = player.getQuestState("Q024_InhabitantsOfTheForestOfTheDead");
			if (st2.isCompleted())
			{
				if (st.getPlayer().getLevel() >= 66)
					htmltext = "31349-01.htm";
			}
			else
				htmltext = "31349-00.htm";
			break;
			
		case State.STARTED:
			switch (npc.getNpcId())
			{
			case Benedict:
				if (cond == 1)
					htmltext = "31349-02.htm";
				else if (cond == 2 || cond == 3)
					htmltext = "31349-04.htm";
				else if (cond == 4)
					htmltext = "31349-10.htm";
				break;
				
			case Wizard:
				if (cond == 2)
				{
					htmltext = "31522-01.htm";
					st.playSound("ItemSound.quest_middle");
					st.set("cond", "3");
					st.giveItems(SuspiciousTotem, 1);
				}
				else if (cond == 3)
					htmltext = "31522-02.htm";
				else if (cond == 5)
					htmltext = "31522-03.htm";
				else if (cond == 6)
					htmltext = "31522-04.htm";
				else if (cond == 9)
				{
					htmltext = "31522-05.htm";
					st.playSound("ItemSound.quest_middle");
					st.set("cond", "10");
				}
				else if (cond == 10)
					htmltext = "31522-05.htm";
				else if (cond == 15)
					htmltext = "31522-06.htm";
				else if (cond == 16)
					htmltext = "31522-13.htm";
				else if (cond == 17)
					htmltext = "31522-16.htm";
				else if (cond == 18)
					htmltext = "31522-14.htm";
				break;
				
			case Agripel:
				if (cond == 4)
					htmltext = "31348-01.htm";
				else if (cond == 5)
					htmltext = "31348-08.htm";
				else if (cond == 16)
					htmltext = "31348-09.htm";
				else if (cond == 17)
					htmltext = "31348-17.htm";
				else if (cond == 18)
					htmltext = "31348-18.htm";
				break;
				
			case Bookshelf:
				if (cond == 6)
					htmltext = "31533-01.htm";
				break;
				
			case Bookshelf2:
				if (cond == 6)
					htmltext = "31534-01.htm";
				break;
				
			case Bookshelf3:
				if (cond == 6 || cond == 7 || cond == 8)
					htmltext = "31535-01.htm";
				else if (cond == 9)
					htmltext = "31535-06.htm";
				break;
				
			case Lidia:
				if (cond == 10)
					htmltext = "31532-01.htm";
				else if (cond == 11 || cond == 12)
					htmltext = "31532-06.htm";
				else if (cond == 13)
				{
					htmltext = "31532-07.htm";
					st.set("cond", "14");
					st.takeItems(Dress, -1);
				}
				else if (cond == 14)
					htmltext = "31532-08.htm";
				else if (cond == 15)
					htmltext = "31532-18.htm";
				else if (cond == 17)
					htmltext = "31532-19.htm";
				else if (cond == 18)
					htmltext = "31532-21.htm";
				break;
				
			case Tombstone:
				if (cond == 11 || cond == 12)
					htmltext = "31531-01.htm";
				else if (cond == 13)
					htmltext = "31531-03.htm";
				break;
				
			case Coffin:
				if (cond == 12)
				{
					htmltext = "31536-01.htm";
					st.giveItems(Dress, 1);
					st.playSound("ItemSound.quest_middle");
					st.set("cond", "13");
					npc.deleteMe();
				}
				break;
			}
			break;
		}
		
		return htmltext;
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return null;
		
		if (st.getInt("cond") == 7)
		{
			st.playSound("ItemSound.quest_itemget");
            st.set("cond", "8");
            npc.broadcastPacket(new NpcSay(npc.getObjectId(), 0, npc.getNpcId(), "You've ended my immortal life! You've protected by the feudal lord, aren't you?"));
            st.giveItems(TotemDoll, 1);
		}
		
		return null;
	}
	
	public static void main(String[] args)
	{
		new Q025_HidingBehindTheTruth(25, qn, "Hiding Behind The Truth");
	}
}